import { updateDrawData } from "@/api/draw";
import { ElMessage } from "element-plus";
import { store, widget } from "@/utils/global";
import { nextTick } from "vue";
import { updateUserStyle } from "@/api/tool";
/**
 * 保存修改的标绘数据
 * @param graphic
 * @returns
 */
export const saveEditorDraw = (graphic: any) => {
    console.log(graphic);
    /**
     * 转换数据
     */
    let postData = JSON.parse(JSON.stringify(graphic.toJSON()));
    /**
     * 转换数据参数
     */
    postData.style = JSON.stringify(postData.style);
    if (postData.position) {
        postData.positions = JSON.stringify(postData.position);
        postData.position = JSON.stringify(postData.position);
    } else {
        postData.positions = JSON.stringify(postData.positions);
    }
    postData.isShow = graphic.show ? "1" : "0";
    if (typeof postData.folderId == "object") {
        postData.folderId = postData.folderId[postData.folderId.length - 1];
    }
    /**
     * 提交数据到数据库
     */
    updateDrawData(postData).then((res) => {
        ElMessage({
            message: "保存成功",
            type: "success",
        });
        widget.isEditorStyles = false;
        // return;
        // 获取树菜单数据
        let treeNode = store.tree.getNode(postData.id).data;
        // 更新数据名称
        treeNode.name = postData.name;
        treeNode.icon = postData.icon;
        console.log(treeNode);
        // 判断是否修改文件夹
        if (treeNode.pid != postData.folderId) {
            store.tree.remove(postData.id);
            // 更新pid
            treeNode.pid = postData.folderId;
            nextTick(() => {
                store.tree.append(treeNode, postData.folderId);
                if (treeNode.show) {
                    store.tree.setChecked(postData.id, true);
                }
            });
        }
        // 判断是否修改自定义样式
        let userStyle = widget.customStyleItem.find((item: any) => item.drawId == postData.id);
        if (userStyle) {
            userStyle.label = postData.name;
            userStyle.pic = postData.icon;
            // console.log(userStyle);
            // return;
            updateUserStyle(userStyle).then((res) => {
                // widget.adobeIllustratorItems = res.data;
                // console.log(res.data);
            });
        }
    });
};
/**
 * 计算三维立体的体积
 */
export const calcVolume = (graphic: any) => {
    const style = graphic.style;
    // 圆周率
    const pi = Math.PI;
    let volume = 0;
    switch (graphic.type) {
        case "box":
            // 长方体或正方体的体积计算   V = l*w*h
            volume = (style.dimensions_x || 0) * (style.dimensions_y || 0) * (style.dimensions_z || 0);
            return volume;

        case "circle":
            // 圆柱的体积计算    V = π*r*r*h
            let c_r = style.radius || 0;
            let c_h = style.diffHeight || 0;
            volume = pi * c_r * c_r * c_h;
            return volume;

        case "ellipse":
            // 椭圆柱的体积计算    V = π*a*b*h
            let e_l_r = style.semiMajorAxis || 0; // 长半轴
            let e_s_r = style.semiMinorAxis || 0; // 短半轴
            let e_h = style.diffHeight || 0; // 高
            volume = pi * e_l_r * e_s_r * e_h;
            return volume;

        case "cylinder":
            // 圆锥的体积计算    V = π*r*r*h/3
            let cy_l_r = style.bottomRadius || 0; // 底部半径
            let cy_h = style.length || 0; // 高
            volume = (pi * cy_l_r * cy_l_r * cy_h) / 3;
            return volume;

        case "ellipsoid":
            let r1 = style.radii_x || 0; // x半径
            let r2 = style.radii_y || 0; // y半径
            let r3 = style.radii_z || 0; // z半径
            if (graphic.options == "radar") {
                // 雷达及半球的体积计算    V = 2/3*π*r*r*r
                volume = (2 / 3) * pi * r1 * r2 * r3;
            } else {
                // 球及椭球的体积计算    V = 4/3*π*r*r*r
                volume = (4 / 3) * pi * r1 * r2 * r3;
            }

            return volume;

        default:
            return 0;
    }
};
